import Footer from '@/components/Footer';
import { getLoginUserUsingGet } from '@/services/backend/userController';
import type { RunTimeLayoutConfig } from '@umijs/max';
import { history, Link } from '@umijs/max';
import defaultSettings from '../config/defaultSettings';
import { AvatarDropdown } from './components/RightContent/AvatarDropdown';
import { requestConfig } from './requestConfig';
import Icon from '@ant-design/icons';
import { IconMap } from '@/components/MenuIcon';
const loginPath = '/user/login';

/**
 * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
 * */
export async function getInitialState(): Promise<InitialState> {
  const initialState: InitialState = {
    currentUser: undefined,
  };
  // 如果不是登录页面，执行
  const { location } = history;
  if (location.pathname !== loginPath) {
    try {
      const res = await getLoginUserUsingGet();
      initialState.currentUser = res.data;
    } catch (error: any) {
      // 如果未登录
      history.push(loginPath);
    }
  }
  return initialState;
}

// ProLayout 支持的api https://procomponents.ant.design/components/layout
// @ts-ignore
export const layout: RunTimeLayoutConfig = ({ initialState }) => {
  return {
    avatarProps: {
      render: () => {
        return <AvatarDropdown />;
      },
    },
    // waterMarkProps: {
    //   content: initialState?.currentUser?.userName,
    // },
    footerRender: () => <Footer />,
    menuHeaderRender: undefined,
    menuItemRender: (menuItemProps, defaultDom) => {
      if (menuItemProps.isUrl || !menuItemProps.path) {
        return defaultDom;
      }
      // 支持二级菜单显示icon
      return (
        <Link to={menuItemProps.path} style={{ display: 'flex', alignItems: 'center' }}>
          {menuItemProps.pro_layout_parentKeys &&
            menuItemProps.pro_layout_parentKeys.length > 0 && (
              // <span style={{ marginRight: '8px' }}>{menuItemProps.icon}</span>
              <span style={{ marginRight: '8px' }}>
                {typeof menuItemProps.icon === 'string' &&
                menuItemProps.icon.indexOf('|custom') > -1 ? (
                  <Icon component={IconMap[menuItemProps.icon.replace('|custom', '')]} />
                ) : (
                  menuItemProps.icon
                )}
              </span>
            )}
          {defaultDom}
        </Link>
      );
    },
    menuDataRender: (menuData: any) => {
      return menuData.map((item: any) => {
        return {
          ...item,
          icon:
            typeof item.icon === 'string' && item.icon.indexOf('|custom') > -1 ? (
              <Icon component={IconMap[item.icon.replace('|custom', '')]} />
            ) : (
              item.icon
            ),
        };
      });
    },
    // logo: `/${logo}`,
    // 自定义 403 页面
    // unAccessible: <div>unAccessible</div>,
    ...defaultSettings,
  };
};

/**
 * @name request 配置，可以配置错误处理
 * 它基于 axios 和 ahooks 的 useRequest 提供了一套统一的网络请求和错误处理方案。
 * @doc https://umijs.org/docs/max/request#配置
 */
export const request = requestConfig;
